﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.InteropServices;
using System.Net;
using System.Net.Sockets;

namespace KeyboardCount
{
    public partial class Login : Form
    {
        UserInfo user = new UserInfo();
        public bool IsLoginSuccess = false;
        public TcpClient LoginTcpClient = null;

        public Login()
        {
            InitializeComponent();

            if (File.Exists("Info.dat"))
            {
                FileStream file = new FileStream("Info.dat", FileMode.Open, FileAccess.Read, FileShare.Read);
                BinaryFormatter b = new BinaryFormatter();
                user = b.Deserialize(file) as UserInfo;
                file.Close();
            }
            
            if (user.IsRemember)
            {
                checkBoxRemember.Checked = true;
                textBoxUserName.Text = user.username;
                textBoxPassword.Text = user.password;
            }

            if (user.IsAutoLogin)
            {
                checkBoxAutoLogin.Checked = true;
            }
        }

        private void buttonLogin_Click(object sender, EventArgs e)
        {
            if (!Check())
            {
                return;
            }

            try
            {
                SendAndRecrive sendRcv = new SendAndRecrive();
                LoginTcpClient = new TcpClient(sendRcv.IP, sendRcv.SRV_PORT);
                sendRcv.SendUsernameAndPasswd(LoginTcpClient, textBoxUserName.Text, textBoxPassword.Text, SendAndRecrive.State.LOGIN);
                if (sendRcv.errno == 0)
                {
                    IsLoginSuccess = true;
                }
                else
                {
                    if (sendRcv.errno == 1)
                        MessageBox.Show("用户名不存在！");
                    else if (sendRcv.errno == 2)
                        MessageBox.Show("密码错误！");
                    else if (sendRcv.errno == 3)
                        MessageBox.Show("该用户已登录！");
                    else
                        MessageBox.Show("登录失败！");
                    
                    IsLoginSuccess = false;                    
                    LoginTcpClient.Close();
                }                
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("登录失败！" + ex.Message);
                return;
            }

            //如果登录成功
            if (IsLoginSuccess)
            {
                user.username = textBoxUserName.Text;
                user.password = textBoxPassword.Text;
                FileStream file = new FileStream("Info.dat", FileMode.Create);
                BinaryFormatter b = new BinaryFormatter();
                b.Serialize(file, user);
                file.Close();
                this.Close();
            }            
        }

        public bool Check()
        {
            if (textBoxUserName.Text.Trim() == "")
            {
                MessageBox.Show("请输入用户名！");
                textBoxUserName.Focus();
                return false;
            }
            if (textBoxPassword.Text == "")
            {
                MessageBox.Show("请输入密码！");
                textBoxPassword.Focus();
                return false;
            }
            return true;
        }

        private void buttonRegister_Click(object sender, EventArgs e)
        {
            Register register = new Register();
            register.ShowDialog();
        }

        private void checkBoxRemember_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxRemember.Checked)
            {
                user.IsRemember = true;
            }
            else
            {
                user.IsRemember = false;
                user.IsAutoLogin = false;
                checkBoxAutoLogin.Checked = false;
            }
        }

        private void checkBoxAutoLogin_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxAutoLogin.Checked)
            {
                user.IsRemember = true;
                user.IsAutoLogin = true;
                checkBoxRemember.Checked = true;
            } 
            else
            {
                user.IsAutoLogin = false;
            }
        }
    }

    [Serializable]
    public class UserInfo
    {
        public string username  = "";
        public string password  = "";
        public bool IsRemember  = false;
        public bool IsAutoLogin = false;
    }
}
